<!--
 * @Author: wjk
 * @Date: 2020-01-09 16:06:59
 * @LastEditors: wjk
 * @LastEditTime: 2020-11-26 13:46:22
 * @description: 商品标题栏
 -->
<template>
  <view class="commodity-option">
    <commodity-group-buy v-if="!hideMenu && allData.group_buying_id" ref="group" :all-data="allData" :querys="query" @rootChange="modulesDataHandle" @takeInGroup="takeInGroup"></commodity-group-buy>
    <commodity-coupon v-if="!hideMenu" ref="couponList" :goods="allData.goods" :all-data="allData" :show-coupon="showCoupon" />
    <commodity-package v-if="commodityType !== 'boxGoods' && allData.goods.type === 2" ref="package" :goods-pack="allData.goods.goods_pack" :goods="allData.goods" :group-buy="group" :buy-count-ary="buyCountAry" @buyCountChange="setBuyCount" @buyCountChangeMax="setBuyCountMax" @confirm="dialogConfirm" />
    <commodity-partner v-if="commodityType === 'partner'" ref="partner" :goods="allData.goods" @validProps="validProps" />
    <commodity-box-goods v-if="commodityType === 'boxGoods'" ref="boxGoods" :all-data="allData" :buy-count-ary="buyCountAry" @validProps="validProps" />
    <commodity-bonus-gift v-if="commodityType === 'bonusGift'" ref="bonusGift" :all-data="allData" @confirm="dialogConfirm" />
    <view class="goods-found">
      <commodity-combo v-if="allData.goods.type === 4" ref="combo" :commodity-type="commodityType" mask-type="combo" :combo-goods="allData.goods" :buy-count-ary="buyCountAry" @selectedItem="modulesDataHandle" @buyCountChange="setBuyCount" @buyCountChangeMax="setBuyCountMax" @confirm="dialogConfirm" />
      <commodity-single
        v-if="(allData.goods.type !== 2 && allData.goods.type !== 4 && commodityType !== 'bonusGift') || commodityType === 'boxGoods'"
        ref="singleGoods"
        :commodity-type="commodityType"
        mask-type="singleGoods"
        :single-goods="allData.goods"
        :group-buy="group"
        :hide-menu="hideMenu"
        :buy-count-ary="buyCountAry"
        @closePopup="closePopup"
        @buyCountChange="setBuyCount"
        @buyCountChangeMax="setBuyCountMax"
        @confirm="dialogConfirm" />
      <view v-if="allData.service_list && allData.service_list.length && commodityType !== 'bonusGift'" class="sLine" @click="showMask('service')">
        <view class="lCont">服务</view>
        <view class="rCont">
          <view v-for="(item, index) in allData.service_list" :key="index" class="serviceItem">
            <block v-if="index < 3">
              <image src="/static/images/commodity/btnshopping__selected.png" />
              <view class="span">{{ item.title }}</view>
            </block>
          </view>
          <image class="right_arror" src="/static/images/icon/icon_arror_right.png" />
        </view>
      </view>
      <commodity-send v-if="!hideMenu && commodityType !== 'bonusGift' && allData.series_type !== 3" ref="send" :all-data="allData" :commodity-type="commodityType" @validProps="validProps" />
      <commodity-box-freight v-if="!hideMenu && commodityType === 'boxGoods'" ref="boxGoodsFreight" :all-data="allData" />
      <view v-if="!hideMenu && commodityType === 'bonusGift'" class="sLine" @click="$navigateTo('/pagesDetail/commodity/freight-calculate?bonus_gift_id=' + allData.bonus_gift_id)">
        <view class="lCont">运费</view>
        <view class="rCont">
          <view class="serviceItem">
            测算明细
          </view>
          <image class="right_arror" src="/static/images/icon/icon_arror_right.png" />
        </view>
      </view>
      <view v-if="commodityType === 'partner'" class="sLine" @click="$navigateTo('https://h5-uat.bisinuolan.cn/H5/#/vipPage/vipRule')">
        <view class="lCont">规则</view>
        <view class="rCont">
          <view class="serviceItem">
            城市合伙人好处
          </view>
          <image class="right_arror" src="/static/images/icon/icon_arror_right.png" />
        </view>
      </view>
    </view>
    <commodity-serve ref="service" :service-list="allData.service_list" />
  </view>
</template>
<script>
import CommodityCoupon from './commodity-coupon'
import CommodityPartner from './commodity-partner'
import CommodityBoxGoods from './commodity-box-goods'
import CommodityBoxFreight from './commodity-box-freight'
import CommodityBonusGift from './commodity-bonus-gift'
import CommoditySend from './commodity-send'
import CommodityPackage from './commodity-package'
import CommodityCombo from './commodity-combo-package'
import CommodityServe from './commodity-serve'
import CommoditySingle from './commodity-single'
import CommodityGroupBuy from './commodity-group-buy'
import { mapGetters } from 'vuex'
export default {
  name: 'CommodityOption',
  components: {
    CommodityCoupon,
    CommodityPartner,
    CommodityBoxGoods,
    CommodityBoxFreight,
    CommodityBonusGift,
    CommoditySend,
    CommodityPackage,
    CommodityCombo,
    CommodityServe,
    CommoditySingle,
    CommodityGroupBuy
  },
  props: {
    allData: {
      type: Object,
      required: true,
      default: () => {
        return {}
      }
    },
    showCoupon: {
      type: Boolean,
      default: false
    },
    hideMenu: {
      type: Boolean,
      default: false
    },
    query: {
      type: Object,
      default: () => { return {} }
    },
    commodityType: {
      type: String,
      default: 'single'
    }
  },
  data() {
    const goods = this.allData.goods
    let min = 1
    let max = 9999
    if (goods) {
      if (goods.min_num) {
        min = goods.min_num
      } else if (goods.goods_pack && goods.goods_pack.min_num) {
        min = goods.goods_pack.min_num
      } else if (goods.goods_pack && goods.goods_pack.limit_num) {
        max = goods.goods_pack.limit_num
      }
    }
    return {
      maskType: '',
      group: {},
      buyCountAry: {
        max: max,
        min: min,
        count: min
      },
      grayStart: [] // 评价灰色星星
    }
  },
  computed: {
    ...mapGetters(['userToken'])
  },
  created() {
    this.initStatus()
  },
  methods: {
    initStatus() {
      // if (this.allData.goods.pack_list && this.allData.goods.pack_list.length && (!this.allData.goods.sku_list || !this.allData.goods.sku_list.length)) {
      //   this.allData.goods.sku_list = this.allData.goods.pack_list.map(item => {
      //     return item
      //   })
      // }
    },
    // 模块数据处理
    modulesDataHandle(json) {
      switch (json.module_name) {
        case 'groupBuy': // 拼团数据处理
          this.group = json.data
          break
        case 'comboPackage':
          // this.allData.freight_template_id = json.data.freight_template_id
          this.allData.goods.freight_template_id = json.data.freight_template_id
          this.allData.goods.status = json.data.status
          this.$forceUpdate(this.allData)
          // this.$refs.couponList.getCouponList(json.data.goods_id)
          this.$refs.send.getAddressList(json.data.freight_template_id)
          break
        default: ''
      }
      this.$emit('modulesData', json)
    },
    // 传递参团操作
    takeInGroup(param) {
      this.$emit('groupHandle', param)
    },
    async dialogConfirm(buyObj) {
      console.log(buyObj);
      if (buyObj.maskType === 'combo') {
        await this.$refs.send.getCalculateArea()
      }
      this.$emit('dialogConfirm', buyObj)
    },
    async showMask(type, limit) {
      console.log("this.commodityType:",this.commodityType);
      console.log("type:",type);
      console.log("limit:",limit);
      if (this.commodityType === 'partner' && limit !== 'toPartnerAddress') {
        const role = await this.$refs.partner.checkRole()
        if (!role) {
          return
        }
      }
      if (this.commodityType === 'bonusGift' && limit === 'buyBtn') {
        this.$refs.bonusGift.checkBuyRole()
        return
      }
      if (this.commodityType === 'boxGoods' && limit === 'buyBtn') {
        this.$refs.singleGoods.showPopup(limit)
        return
      }
      this.maskType = type
      if ((limit === 'buyBtn' || limit === 'cartBtn' || limit === 'openGroup' || limit === 'groupSingleBuy') && !this.userToken) {
        this.$navigateTo('/pages/login/index')
        return
      }
      if (limit === 'openGroup' || limit === 'groupSingleBuy') {
        // return
      }
      this.$refs[type].showPopup(limit)
    },
    validProps(obj) {
      if (this.$refs.boxGoodsFreight) {
        this.$refs.boxGoodsFreight.setAddress(obj)
      }
      this.$emit('validProps', obj)
    },
    closePopup(obj) {
      if (this.$refs.boxGoodsFreight) {
        this.$refs.boxGoodsFreight.setGoods(obj)
      }
    },
    setBuyCount(num) {
      const count = this.buyCountAry.min > num ? this.buyCountAry.min : num
      this.$set(this.buyCountAry, 'count', count)
    },
    setBuyCountMax(num) {
      if (!this.group.groupInfo) { return }
      this.$set(this.buyCountAry, 'max', num)
      if (this.buyCountAry.count > num) {
        this.$set(this.buyCountAry, 'count', num)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.commodity-option{
  position: relative;
  // z-index: 3;
}
</style>
